package com.lgCore.util;/**
 * Created by by on 2020/12/8 13:48
 */

import java.io.*;
import java.util.Date;

/**
 * obj转byte数组
 *@program: sxreader_rpc
 *@author: ligang
 *@create: 2020-12-08 13:48
 */
public class ByteObjUtil {
    public static byte[] objToBytes(Object object){
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        ObjectOutputStream oo = null;
        try {
            oo = new ObjectOutputStream(bo);
            oo.writeObject(object);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                oo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bo.toByteArray();
    }


    public static Object bytesToObject(byte[] bytes){
        ObjectInputStream sIn =null;
        ByteArrayInputStream in=null;
        try {
            in = new ByteArrayInputStream(bytes);
            sIn = new ObjectInputStream(in);
            return sIn.readObject();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                sIn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


}
